//
//  Indicator.m
//  JxCarios
//
//  Created by xcq on 15/1/30.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "Indicator.h"

@implementation Indicator{
    @package
    NSInteger _animationTime;
    CGRect _firstRect;
    CGRect _lastRect;
    CGFloat _indicatorW;
    NSTimer *_autoScrollTimer;
}


- (instancetype)initWithFrame:(CGRect)frame andPage:(NSInteger)page and:(id)supView andBackGroundColor:(UIColor *)color{
     UIViewController *view = (UIViewController *)supView;
    CGFloat width = view.view.frame.size.width;
    CGFloat indicatorW = width / page;
    _indicatorW = indicatorW;
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, indicatorW, frame.size.height)];
    if (self) {
        self.pageNum = page != 0 ? page : 1;
        self.currenNum = 0;
        self.backgroundColor = color;
        self.topView = supView;
    }
    return self;
}
-(void)shouldAutoShow:(BOOL)autoStart
{
    _firstRect = self.frame;
    if (autoStart) {
        if ([_autoScrollTimer isValid]) {
            
        }else{
            _autoScrollTimer=[NSTimer scheduledTimerWithTimeInterval:HOMEPAGEVIEWSCORLLETIME target:self selector:@selector(startAnimationScroll) userInfo:nil repeats:YES];
        }
    }else{
        if ([_autoScrollTimer isValid]) {
            [_autoScrollTimer invalidate];
            _autoScrollTimer = nil;
        }
    }
}
- (void)startAnimationScroll{
  
    [UIView animateWithDuration:.3f animations:^{
        _currenNum ++;
        if (_currenNum > _pageNum-1) {
            _currenNum = 0;
        }
        self.frame = CGRectMake(_firstRect.origin.x + (_currenNum * _indicatorW), _firstRect.origin.y, _firstRect.size.width, _firstRect.size.height);
        
     }];
}

@end
